Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

const Ci = Components.interfaces;
const Cc = Components.classes;

function Faviconbadge() {
  this.wrappedJSObject = this;
}

Faviconbadge.prototype = {
  classDescription: "",
  classID: Components.ID("{da4f0a54-9ba7-4a3c-bdc3-b44043f2dab6}"),
  contractID: "@faviconbadge.menjatallarins.com/faviconbadge;1",
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFaviconBadge, Ci.nsIClassInfo]),
  _xpcom_categories: [{category: "JavaScript global property", entry: "fluid"},
                      {category: "JavaScript global property", entry: "platform"}],
  implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
  flags: Ci.nsIClassInfo.DOM_OBJECT,
  getInterfaces: function(countRef) {
     var interfaces = [Ci.nsIFaviconBadge, Ci.nsIClassInfo, Ci.nsISupports];
     countRef.value = interfaces.length;
     return interfaces;
  },
  getHelperForLanguage: function(count) {return null;},
  
  dockBadge : "",
  
  badgets : [],

  reloadBadgets : function() {
    this.badgets=[];
    this.loadBadgets();
  },
	
  getBadgets : function() {
    if (this.badgets.length == 0) {
            this.loadBadgets();
    }
    return this.badgets;
  },
	
  loadBadgets : function() {
    var file = Cc["@mozilla.org/file/directory_service;1"]
                         .getService(Ci.nsIProperties)
                         .get("ProfD", Ci.nsIFile);
    file.append("badgets");
  
    // file is the given directory (nsIFile)
    var entries = file.directoryEntries;
    var array = [];
    
    while(entries.hasMoreElements()) {
      var entry = entries.getNext();
      var badget = {
        "includes" : []
      }	

      try {
        entry.QueryInterface(Ci.nsIFile);
  
        if (!entry.isFile()) {
          continue;
        }
        
        dump("Loading : '"+entry.leafName+"'\n");
  
        badget.file = entry.leafName;
        badget.enabled = true;
        
        // open an input stream from file
        var istream = Cc["@mozilla.org/network/file-input-stream;1"]
                                .createInstance(Ci.nsIFileInputStream);
        istream.init(entry, 0x01, 0444, 0);
        istream.QueryInterface(Ci.nsILineInputStream);
        
        var line = {}, lines = [], hasmore;
        do {
          hasmore = istream.readLine(line);
          lines.push(line.value); 
    
          var match = line.value.match(/\/\/ \@(\S+)\s+([^\n]+)/);
          if (match != null) {
            switch (match[1]) {
              case "name":
              case "description":
                badget[match[1]] = match[2];
                break;
              case "include":
                badget[match[1]+"s"].push(match[2]);
                break;
            }
          }
        } while(hasmore);

      }catch(e){
        dump("ERROR: " + e);
      }
      
      istream.close();
      this.badgets.push(badget);
    }
  }
	
}

function NSGetModule(aCompMgr, aFileSpec) { 
  return XPCOMUtils.generateModule([Faviconbadge]);
}

